/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/

package test;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EsbEncryptUtil {

	private static final String Algorithm = "DESede";
	private static final String sKey = "hbIf32Eco";
	private static final String dKey = "cejFalk3fAw3rp2L3d2c3o23";
	private static final BASE64Encoder Encoder = new BASE64Encoder();
	private static final BASE64Decoder Decoder = new BASE64Decoder();

	public static final String getKey() {
		return "hbIf32Eco";
	}

	public static final String getDKey() {
		return "cejFalk3fAw3rp2L3d2c3o23";
	}

	public static final String encode(String input, String sKey,String charset) throws Exception
    {
        javax.crypto.SecretKey deskey;
        byte key[] = null == charset ?sKey.getBytes():sKey.getBytes(charset);
        deskey = new SecretKeySpec(key, "DESede");
        byte cipherByte[];
        Cipher c1 = Cipher.getInstance("DESede");
        c1.init(1, deskey);
        cipherByte = c1.doFinal(null == charset ?input.getBytes():input.getBytes(charset));
        return Encoder.encode(cipherByte);
    }

	public static final String decode(String input, String sKey,String charset) throws Exception
    {
        javax.crypto.SecretKey deskey;
        byte key[] = null == charset ?sKey.getBytes():sKey.getBytes(charset);
        deskey = new SecretKeySpec(key, "DESede");
        byte clearByte[];
        Cipher c1 = Cipher.getInstance("DESede");
        c1.init(2, deskey);
        clearByte = c1.doFinal(Decoder.decodeBuffer(input));
        return null == charset ?new String(clearByte):new String(clearByte,charset);
    }

	public static String byteToHexStr(byte paramArrayOfByte[]) {
		StringBuffer localStringBuffer = new StringBuffer(
				paramArrayOfByte.length);
		for (int i = 0; i < paramArrayOfByte.length; i++) {
			String str = Integer.toHexString(255 & paramArrayOfByte[i]);
			if (str.length() < 2)
				localStringBuffer.append(0);
			localStringBuffer.append(str.toUpperCase());
		}

		return localStringBuffer.toString();
	}

	public static byte[] HexStrtoByte(String hexstr) {
		String str = "0123456789ABCDEF";
		char hexs[] = hexstr.toCharArray();
		byte bytes[] = new byte[hexstr.length() / 2];
		for (int i = 0; i < bytes.length; i++) {
			int n = str.indexOf(hexs[2 * i]) * 16;
			n += str.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (n & 255);
		}

		return bytes;
	}

}
